using UnityEngine;
using System.Collections;
using MakingThings;

public class MyOSCHandler : MonoBehaviour {

	UdpPacket udpPacket;
	Osc oscUdp;
	
	float r_head_x;
	float r_head_y;
	float r_head_z;
		 		 
	 float r_leftUArm_x;
	float r_leftUArm_y;
	float r_leftUArm_z;
			 
	 float r_leftLArm_x;
	float r_leftLArm_y;
	float r_leftLArm_z;
	
	 float r_rightUArm_x;
	float r_rightUArm_y;
	float r_rightUArm_z;
	
	float r_rightLArm_x;
	float r_rightLArm_y;
	float r_rightLArm_z;
	 
	 float r_hip_x;
	 float r_hip_y;
	 float r_hip_z;
	 
	 float r_leftULeg_x;
	 float r_leftULeg_y;
	 float r_leftULeg_z;
	 
	 float r_leftLLeg_x;
	 float r_leftLLeg_y;
	 float r_leftLLeg_z;
	 	 
	 float r_rightULeg_x;
	 float r_rightULeg_y;
	 float r_rightULeg_z;
	 
	 float r_rightLLeg_x;
	 float r_rightLLeg_y;
	 float r_rightLLeg_z;
	
	// The method you specified as the handler will be called back when a 
	 // message with a matching address string comes back from the board.
	 public void MyOscMessageHandler(OscMessage oscMessage)
	 {
         float scale=100.0f;
		 
		 r_head_x = scale*(float)oscMessage.Values[0];
		 r_head_y = scale*(float)oscMessage.Values[1];
		 r_head_z = scale*(float)oscMessage.Values[2];
		 		 
		 r_leftUArm_x= scale*(float)oscMessage.Values[3];
		 r_leftUArm_y= scale*(float)oscMessage.Values[4];
		 r_leftUArm_z= scale*(float)oscMessage.Values[5];
		 
		 r_leftLArm_x = (float)oscMessage.Values[6];
		 r_leftLArm_y = (float)oscMessage.Values[7];
		 r_leftLArm_z = (float)oscMessage.Values[8];
		 		 
		 r_rightUArm_x = (float)oscMessage.Values[9];
		 r_rightUArm_y = (float)oscMessage.Values[10];
		 r_rightUArm_z = (float)oscMessage.Values[11];
		 		 
		 r_rightLArm_x = (float)oscMessage.Values[12];
		 r_rightLArm_y = (float)oscMessage.Values[13];
		 r_rightLArm_z = (float)oscMessage.Values[14];
		 		 
		 r_hip_x = (float)oscMessage.Values[15];
		 r_hip_y = (float)oscMessage.Values[16];
		 r_hip_z = (float)oscMessage.Values[17];
		 
		 r_leftULeg_x = (float)oscMessage.Values[18];
		 r_leftULeg_y = (float)oscMessage.Values[19];
		 r_leftULeg_z = (float)oscMessage.Values[20];
		 		 		 
		 r_leftLLeg_x = (float)oscMessage.Values[21];
		 r_leftLLeg_y = (float)oscMessage.Values[22];
		 r_leftLLeg_z = (float)oscMessage.Values[23];
		 		 		 
		 r_rightULeg_x = (float)oscMessage.Values[24];
		 r_rightULeg_y = (float)oscMessage.Values[25];
		 r_rightULeg_z = (float)oscMessage.Values[26];
		 		 
		 r_rightLLeg_x = (float)oscMessage.Values[27];
		 r_rightLLeg_y = (float)oscMessage.Values[28];
		 r_rightLLeg_z = (float)oscMessage.Values[29];
		  
		 // write the message to a console, for example
		 //print("AIn0 > " + Osc.OscMessageToString(oscMessage));
	 } 
	 
	  public void MyOtherOscMessageHandler(OscMessage oscMessage)
	 {
          
		 // write the message to a console, for example
		 //print("Got me a message: "+Osc.OscMessageToString(oscMessage));
		 //GameObject.Find("leftarm").transform.Rotate(0,1,0);
	 } 
	
	 // Use this for initialization
	void Start () {
		print("BEGIN");
		 udpPacket = new UdpPacket("127.0.0.1",12000,31841);

	     oscUdp = new Osc(udpPacket);
		 // A thread is started when the Osc object is created to read 
		 // incoming messages.
		string msg = "/pilot/vector3f/vector3f/vector3f/vector3f/vector3f/vector3f/vector3f/vector3f/vector3f/vector3f";
		
		oscUdp.SetAddressHandler(msg, MyOscMessageHandler);
		

	 }


	void Update () {
		
		GameObject gameObj;
		
		gameObj = GameObject.Find("head");
		gameObj.transform.eulerAngles = new Vector3(
			r_head_x,
			r_head_y,
			r_head_z
		);
		
		gameObj = GameObject.Find("leftShould");
		gameObj.transform.eulerAngles = new Vector3(
			r_leftUArm_x,
			r_leftUArm_y,
			r_leftUArm_z
		);
		
		gameObj = GameObject.Find("leftArm");
		gameObj.transform.eulerAngles = new Vector3(
			r_leftLArm_x,
			r_leftLArm_y,
			r_leftLArm_z
		);
		
		gameObj = GameObject.Find("rightShould");
		gameObj.transform.eulerAngles = new Vector3(
			r_rightUArm_x,
			r_rightUArm_y,
			r_rightUArm_z
		);
		
		gameObj = GameObject.Find("rightArm");
		gameObj.transform.eulerAngles = new Vector3(
			r_rightLArm_x,
			r_rightLArm_y,
			r_rightLArm_z
		);
		
		gameObj = GameObject.Find("leftLeg");
		gameObj.transform.eulerAngles = new Vector3(
			r_leftLLeg_x,
			r_leftLLeg_y,
			r_leftLLeg_z
		);
		
		gameObj = GameObject.Find("leftThigh");
		gameObj.transform.eulerAngles = new Vector3(
			r_leftULeg_x,
			r_leftULeg_y,
			r_leftULeg_z
		);
		
		gameObj = GameObject.Find("rightLeg");
		gameObj.transform.eulerAngles = new Vector3(
			r_rightLLeg_x,
			r_rightLLeg_y,
			r_rightLLeg_z
		);
		
		gameObj = GameObject.Find("rightThigh");
		gameObj.transform.eulerAngles = new Vector3(
			r_rightULeg_x,
			r_rightULeg_y,
			r_rightULeg_z
		);
		
		//GameObject.Find("leftArm").transform.Rotate(0,1,0);
	}
}
